package gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import fn.Latex;

public class ToolCreate extends Tool {

	boolean namingNode, connectingBranch, namingBranch;
	Point p = new Point(0, 0);
	UI_Node n = null, n0 = null, n1 = null;
	UI_Branch b = null;

	@Override
	public void select(MainFrame mainFrame) {
		mainFrame.status("Create tool selected...");
	}

	@Override
	public void keyType(MainFrame f, UI_SFG graph, KeyEvent e, boolean done) {
		// DONE
		if (done) {
			try {
				// branch
				if (namingBranch) {
					b.setFn(f.getInput());
					namingBranch = false;
					f.stopInput();
					b = null;
					n = null;
				} else if (connectingBranch) {
					connectingBranch = false;
					b = null;
					n = null;
					n0 = null;
					n1 = null;
				}
				// node
				else if (namingNode) {
					if (n.name().replaceAll("\t\n \\\\", "").length() == 0)
						throw new Exception();
					Latex.verify(n.name());
					if(graph.contains(n))
						throw new Exception("Node already exists.");
					graph.addNode(n);
					namingBranch = false;
					namingNode = false;
					f.stopInput();
					b = null;
					n = null;
				}
			} catch (Exception e1) {
				f.startInput("Wrong input. Try again");
			}
		}
		// STILL INPUTTING
		else {
			// branch
			if (namingBranch) {
				try {
					b.setFn(f.getInput());
				} catch (Exception ex) {}
			}
			// node
			else if (namingNode) {
				n.setName(f.getInput());
			}
		}
	}

	@Override
	public void clear(MainFrame f, UI_SFG graph) {
		namingBranch = false;
		connectingBranch = false;
		namingNode = false;
		if (graph.isConnected(n0, n1)) {
			graph.disconnect(n0, n1);
		}
		n0 = null;
		n1 = null;
		n = null;
		b = null;
		f.stopInput();
	}

	@Override
	public void mouseClick(MainFrame f, UI_SFG graph, MouseEvent e, MyView v) {
		int btn = e.getButton();
		if (btn == MouseEvent.BUTTON1) {
			// BRANCH
			if (namingBranch) {
				keyType(f, graph, null, true);
			} else if (n1 != null) {
				if (!connectingBranch) {
					n0 = n1;
					b = new UI_Branch(graph, n0, n1);
					n1 = new UI_Node(p);
					connectingBranch = true;
					mouseMove(f, graph, e, 0, 0, v);
				} else {
					graph.connect(n0, n1, b = new UI_Branch(graph, n0, n1));
					connectingBranch = false;
					f.startInput("Input gain");
					namingBranch = true;
				}
			}
			// NODE
			else if (!connectingBranch) {
				if (n != null)
					keyType(f, graph, null, true);
				n = new UI_Node(p);
				p = new Point(e.getX(), e.getY());
				f.startInput("Input node name");
				namingNode = true;
			}
		} else if (btn == MouseEvent.BUTTON3)
			clear(f, graph);
	}

	@Override
	public void mouseMove(MainFrame f, UI_SFG graph, MouseEvent e, int dX, int dY, MyView v) {
		p.x = e.getX();
		p.y = e.getY();
		UI_Node snap = snap(f, graph, p, null);
		if (snap != null)
			n1 = snap;
		else if (!namingBranch)
			n1 = null;
	}

	@Override
	public void mouseDrag(MainFrame f, UI_SFG graph, MouseEvent e, int dX, int dY, MyView v) {
		p.x = e.getX();
		p.y = e.getY();
		UI_Node snap = snap(f, graph, p, null);
		if (snap != null)
			n1 = snap;
		else if (!namingBranch)
			n1 = null;
	}

	@Override
	public Cursor cursor() {
		if (n1 != null)
			return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
		if (namingBranch || namingNode)
			return Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
		else
			return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
	}

	@Override
	public void paint(MainFrame mainFrame, UI_SFG graph, Graphics2D g, MyView v) {
		if (n != null) {
			g.setColor(Color.GREEN.darker());
			n.paint(g, v);
		}
		if (b != null) {
			if (connectingBranch)
				b.set(graph, n0, new UI_Node(p));
			g.setColor(Color.GREEN.darker());
			b.paint(g, v);
		}
		g.setColor(Color.RED);
	}

	@Override
	public boolean isBusy() {
		return namingBranch || namingNode || connectingBranch;
	}

}
